package com.ny.controller;

import com.ny.entity.Result;
import com.ny.entity.Device;
import com.ny.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/device")
public class DeviceController {
    
    @Autowired
    private DeviceService deviceService;
    
    /**
     * 查询所有设备信息
     * URL: GET /device/findAll
     */
    @GetMapping("/findAll")
    public Result findAllDevices() {
        List<Device> devices = deviceService.findAllDevices();
        return Result.success(devices);
    }
    
    /**
     * 根据设备名称查询设备信息
     * URL: GET /device/findByName?deviceName=xxx
     */
    @GetMapping("/findByName")
    public Result findDevicesByName(@RequestParam String deviceName) {
        List<Device> devices = deviceService.findDevicesByName(deviceName);
        return Result.success(devices);
    }
    
    /**
     * 根据设备ID删除设备信息
     * URL: DELETE /device/delete/{deviceId}
     */
    @DeleteMapping("/delete/{deviceId}")
    public Result deleteDeviceById(@PathVariable int deviceId) {
        int result = deviceService.deleteDeviceById(deviceId);
        if (result > 0) {
            return Result.success("设备删除成功");
        } else {
            return Result.error("设备删除失败");
        }
    }
    
    /**
     * 根据设备ID删除设备信息 (POST方法版本)
     * URL: POST /device/deleteById?deviceId=xxx
     */
    @PostMapping("/deleteById")
    public Result deleteDeviceByIdPost(@RequestParam int deviceId) {
        int result = deviceService.deleteDeviceById(deviceId);
        if (result > 0) {
            return Result.success("设备删除成功");
        } else {
            return Result.error("设备删除失败");
        }
    }
}